package com.ali.framework.gateway.filter;

import org.apache.commons.logging.Log;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.io.PrintStream;
import java.time.OffsetTime;
import java.util.List;

/**
 * @Author: Smile
 * @Date: 2023/12/15/13:56
 * @Description:
 */
@Component
public class GatewayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String first = exchange.getRequest().getQueryParams().getFirst("user");
        if (first == null) {
            PrintStream out = System.out;
            out.println("first is null");
            //返回一个消息码
            exchange.getResponse().setStatusCode(HttpStatus.valueOf(401));
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    /**
     * 过滤器的优先级越小越优先
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
